#include <iostream>
#include <Windows.h>

int main()
{
	using namespace std;
	cout << "Creating an instance of a named pipe..." << endl;
 
    // Create a pipe to send data
    HANDLE pipe = CreateNamedPipe(
        L"\\\\.\\pipe\\my_pipe", // name of the pipe
        PIPE_ACCESS_OUTBOUND, // 1-way pipe -- send only
        PIPE_TYPE_BYTE, // send data as a byte stream
        1, // only allow 1 instance of this pipe
        0, // no outbound buffer
        0, // no inbound buffer
        0, // use default wait time
        NULL // use default security attributes
    );
 
    if (pipe == NULL || pipe == INVALID_HANDLE_VALUE) {
        cout << "Failed to create outbound pipe instance.";
        // look up error code here using GetLastError()
        system("pause");
        return 1;
    }
 
    wcout << "Waiting for a client to connect to the pipe..." << endl;
 
    // This call blocks until a client process connects to the pipe
    BOOL result = ConnectNamedPipe(pipe, NULL);
    if (!result) {
        cout << "Failed to make connection on named pipe." << endl;
        // look up error code here using GetLastError()
        CloseHandle(pipe); // close the pipe
        system("pause");
        return 1;
    }
 
    cout << "Sending data to pipe..." << endl;
 
    // This call blocks until a client process reads all the data
    const char *data = "*** Hello Pipe World ***";
    DWORD numBytesWritten = 0;
    result = WriteFile(
        pipe, // handle to our outbound pipe
        data, // data to send
        strlen(data) * sizeof(char), // length of data to send (bytes)
        &numBytesWritten, // will store actual amount of data sent
        NULL // not using overlapped IO
    );
 
 
    if (result) {
        cout << "Number of bytes sent: " << numBytesWritten << endl;
    } else {
        cout << "Failed to send data." << endl;
        // look up error code here using GetLastError()
    }
 
    // Close the pipe (automatically disconnects client too)
    CloseHandle(pipe);
 
    cout << "Done. Press any key to continue" << endl;
 
    system("pause");
    return 0;
}